package com.tanhao.takeaway.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhao.takeaway.commom.CustomException;
import com.tanhao.takeaway.entity.category;
import com.tanhao.takeaway.entity.dish;
import com.tanhao.takeaway.entity.setmeal;
import com.tanhao.takeaway.mapper.categoryMapper;
import com.tanhao.takeaway.service.categoryService;
import com.tanhao.takeaway.service.dishService;
import com.tanhao.takeaway.service.setmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class categoryServiceImpl extends ServiceImpl<categoryMapper,category> implements categoryService {

    @Autowired
    private dishService dishservice;

    @Autowired
    private setmealService setmealservice;



    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<dish> lambdaQueryWrapper=new LambdaQueryWrapper<dish>();
        lambdaQueryWrapper.eq(dish::getCategoryId,id);
        int count1 = dishservice.count(lambdaQueryWrapper);
        LambdaQueryWrapper<setmeal> lambdaQueryWrapper1=new LambdaQueryWrapper<setmeal>();
        lambdaQueryWrapper1.eq(setmeal::getCategoryId,id);
        int count2 = setmealservice.count(lambdaQueryWrapper1);

        if(count1>0){
           throw new CustomException("当前分类关联了菜品，不能删除");

        }

        if(count2>0){
            throw new CustomException("当前分类关联了套餐，不能删除");

        }

        super.removeById(id);
    }
}
